home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17195 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  63 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: int::~int()
  5. Message-ID: <marnoldDpuDu9.D7s@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <317083F7.116E@public.sta.net.cn>
  8. Date: Sun, 14 Apr 1996 07:51:45 GMT
  9. Sender: marnold@netcom13.netcom.com
  10.  
  11. Xu Yifeng <jafd@public.sta.net.cn> writes:
  12.  
  13. >Hi everybody,
  14.  
  15. >can your compiler compile following program?
  16.  
  17. >//------------------------
  18. >void main()
  19. >{
  20. >     int i = 0;
  21.  
  22. >     i.int::~int();
  23. >}
  24. >//------------------------
  25.  
  26. >is it a legal C++ program?
  27.  
  28. It's supposed to be, but most C++ compiler's will not compile it.
  29.  
  30. In fact, this exact sitution is encountered in the Standard Template
  31. Library (STL), which contains the following template function...
  32.  
  33. template <class T>
  34. inline void destroy(T* pointer) {
  35.     pointer->~T();
  36. }
  37.  
  38. ...which is supposed to be used to destroy anything, including an
  39. instance of an int, just like your sample code wants to.
  40.  
  41. To make STL work with current compilers, you'll find it also contains
  42. work-around versions of destroy().  There's one specifically for int,
  43. and it looks like this...
  44.  
  45. inline void destroy(int*) {}
  46.  
  47. ...which is what the template version is supposed to do; nothing.  STL
  48. contains about 50 or so of these specific versions of destroy() (for
  49. ints, chars, floats, etc.).
  50.  
  51. You sample code is legal as far as the language goes, but probably
  52. not as far as your compiler is concerned.  Mine, Borland C++ 4.52,
  53. will not compile it either.  However, I think Borland C++ 5.0 will.
  54.  
  55. Regards,
  56. -------------------------------------------------------------------------
  57. Matt Arnold                       |        | ||| | |||| |  | | || ||
  58. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  59. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  60. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  61. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  62. -------------------------------------------------------------------------
  63.